<script>
/**
 * 可以改变指定字段的列表显示，使其可以通过点击拨打点击的电话号码。还能通过点击按钮，依据指定配置远程获取电话号码并进行拨打
 * 改变列表显示的方法是通过所有者的额外渲染方法 addExtRender 指定渲染字段渲染组件，并通过 importTableComponents 为所有者显示组件，达到使用指定组件渲染列表的结果
 * 由于对象可能是gp或者salve，owner对应的组件级别不同，importTableComponents 需要在组件内部进行传递的处理，在gp中需要将组件交给下级的grid才能正常运作（addExtRender处理亦类似）
 */
const call_path = '/service/{app}/desensitization/{id}/1';
const display_path = '/service/{app}/desensitization/display/{id}/1';
import base from '../actionBase'
import PhoneField from './field'
import $ from '@/common'
export default {
    extends: base,
    data() {
        return {
            buttonType: 'success'
        }
    },
    methods: {
        actionTrigger(data, row, index, cb, option) {
            var param = {
                data,
                row,
                index,
                cb,
                option
            }
            if (option) {
                //行内field发送的处理,使用参数inline分辨
                this.callNumber(option.number, param, row)
            } else if (this.actionModel.param.path_phone) {
                return $.link(this.actionModel.param.path_phone, row, {
                    param: row
                }).then((res) => {
                    this.callNumber(res.data, param)
                })
            }
        },
        callNumber(num, data, row) {
            //触发指定action
            var param = this.actionModel.param

            // garbadge code
            window.uniquerow_data = row;
            // garbadge code

            $.webcall.callNumber(num, () => {
                if (param.on_get) {
                    this.$owner.actionTrigger({
                        ...data,
                        code: param.on_get
                    })
                }
            }, () => {
                if (param.on_loss) {
                    this.$owner.actionTrigger({
                        ...data,
                        code: param.on_loss
                    })
                }
            })
        }
    },
    created() {
        var model = this.actionModel
        if (model.param.field_phone) {
            let fieldRender = {}
            let arr = model.param.field_phone.split(';')


            arr && arr.forEach((code) => {
                fieldRender[$.trim(code)] = {
                    component: 'PhoneField',
                    action: model.code,
                    call_path: model.param.call_path || call_path,
                    display_path: model.param.display_path || display_path,
                    buttons: $.resolveButtons(model.param.buttons)
                }
            })
            this.$owner.addExtRender(fieldRender)
            this.$owner.importTableComponents({
                PhoneField
            })
        }
        if (model.param.path_phone) {
            this.$owner.setInlineActionStatus({
                code: model.code,
                show: true
            })
        } else {
            this.actionHidden = true
            this.$owner.setInlineActionStatus({
                code: model.code,
                show: false
            })
        }
    }
}
</script>
